﻿#include "DAStandardGraphicsTextItem.h"

#include <QFont>
#include <QDebug>
#include <QTextCursor>
#include <QGraphicsSceneMouseEvent>
//===================================================
// using DA namespace -- 禁止在头文件using！！
//===================================================

using namespace DA;

//===================================================
// DAStandardGraphicsTextItem
//===================================================
DAStandardGraphicsTextItem::DAStandardGraphicsTextItem(QGraphicsItem* parent)
{
    initItem();
}

DAStandardGraphicsTextItem::DAStandardGraphicsTextItem(const QString& str, const QFont& f, QGraphicsItem* parent)
    : QGraphicsTextItem(parent)
{
    initItem();
    setPlainText(str);
    setFont(f);
}

DAStandardGraphicsTextItem::DAStandardGraphicsTextItem(const QFont& f, QGraphicsItem* parent)
    : QGraphicsTextItem(parent)
{
    initItem();
    setFont(f);
}

/**
 * @brief 设置编辑模式
 * @param on
 */
void DAStandardGraphicsTextItem::setEditMode(bool on)
{
    setTextInteractionFlags(on ? Qt::TextEditorInteraction : Qt::NoTextInteraction);
}

void DAStandardGraphicsTextItem::focusOutEvent(QFocusEvent* focusEvent)
{
    //在有选中的情况下，把选中的内容取消
    QTextCursor cursor = textCursor();
    cursor.clearSelection();
    cursor.setPosition(QTextCursor::Start);
    setTextCursor(cursor);
    setTextInteractionFlags(Qt::NoTextInteraction);
    QGraphicsTextItem::focusOutEvent(focusEvent);
}

void DAStandardGraphicsTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
    if (event->button() == Qt::LeftButton) {
        // czy:setTextInteractionFlags必须在setFocus之前，否则会出现异常
        setTextInteractionFlags(Qt::TextEditorInteraction);
        setFocus();
    }
    QGraphicsTextItem::mouseDoubleClickEvent(event);
}

void DAStandardGraphicsTextItem::initItem()
{
    setDefaultTextColor(Qt::black);  //设置字体颜色
    setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    setTextInteractionFlags(Qt::TextEditorInteraction);
}
